#include "dialog_game.h"
#include "ui_dialog_game.h"
#include "xianshi.h"
#include <QMessageBox>
#include "dialog_fenshu.h"

Dialog_game::Dialog_game(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Dialog_game)
{
    ui->setupUi(this);

    if(open_file("/usr/share/shuoxie/model/handwrite.model")){
    hz_index=0;

    han_zi:: hanzi* my_zi;
        xiecharacter.initSize(500,500);
       my_zi=new han_zi::hanzi(NULL,&xiecharacter);
        my_zi->setMinimumSize(500,500);
        my_zi->setMaximumSize(500,500);
        my_zi->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
        my_zi->isBox=true;
        my_zi->isDispStr=false;
        my_zi->isLine=true;
        while(1){
            int num=ui->gridLayout_3->count();
            if(!num) break;
            QWidget *p=ui->gridLayout_3->itemAt(num-1)->widget();
            ui->gridLayout_3->removeWidget(p);
            delete p;
        }
        ui->gridLayout_3->addWidget(my_zi);

        show_hanzi(NULL);
    }
    else
        this->close();
}

Dialog_game::~Dialog_game()
{
    myrecognizer.clear();
    delete ui;
}


bool Dialog_game::open_file(QString file)
{
    bool b=false;
    if(file.length()>0){
    QByteArray ba = file.toLatin1();
        char *mm = ba.data();

        myrecognizer.clear();
    if( myrecognizer.openFile(mm)){

        b=true;
    }

    }
    return b;
}

void Dialog_game::on_bt_next_clicked()
{
    QString str=ui->le_hanzi->text();
    //printf("%s,%d\n",str,str.length());
    if(hz_index>=str.length())
    {
        hz_index=0;
    }
    if(str.length()>0)
    {
        QString s=str.at(hz_index);
        displayCharacter=myrecognizer.findCharacter(s.toStdString());
        if(displayCharacter!=NULL){
            //printf("%s,%d\n",ch->word,ch->strokeCount);
            show_hanzi(displayCharacter);

        }
        hz_index++;
    }
}

void Dialog_game::show_hanzi(HandWriteRecognizer::Character* mycharacter)
{


        han_zi:: hanzi* my_zi;

           my_zi=new han_zi::hanzi(NULL,mycharacter);

            my_zi->setMinimumSize(500,500);
            my_zi->setMaximumSize(500,500);
            my_zi->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
            my_zi->isBox=true;
            my_zi->isDispStr=true;
            while(1){
                int num=ui->gridLayout_2->count();
                if(!num) break;
                QWidget *p=ui->gridLayout_2->itemAt(num-1)->widget();
                ui->gridLayout_2->removeWidget(p);
                delete p;
            }
            ui->gridLayout_2->addWidget(my_zi);



    this->update();
}

void Dialog_game::on_bt_chonghua_clicked()
{
    han_zi:: hanzi* my_zi;
    xiecharacter.clear();
        xiecharacter.initSize(500,500);
       my_zi=new han_zi::hanzi(NULL,&xiecharacter);
        my_zi->setMinimumSize(500,500);
        my_zi->setMaximumSize(500,500);
        my_zi->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
        my_zi->isBox=true;
        my_zi->isDispStr=false;
        my_zi->isLine=true;
        while(1){
            int num=ui->gridLayout_3->count();
            if(!num) break;
            QWidget *p=ui->gridLayout_3->itemAt(num-1)->widget();
            ui->gridLayout_3->removeWidget(p);
            delete p;
        }
        ui->gridLayout_3->addWidget(my_zi);
}

void Dialog_game::on_bt_pinfen_clicked()
{
    if(displayCharacter!=NULL)
    {
        try{
        if(displayCharacter->strokeCount>0){
        int s=myrecognizer.getDirection(&xiecharacter);
        int d=myrecognizer.getDirection(displayCharacter);
        int v=std::abs(s-d);
        double val1=1-((double)v/(d+s)/2);
        int val=val1*100;
        if(displayCharacter->strokeCount!=xiecharacter.strokeCount)
            val=0;
        char str[128];
        sprintf(str,"%d",val);
        //QMessageBox::information(NULL, "得分",str , QMessageBox::Ok , QMessageBox::Ok);
        //printf("%d:%d,%d,%f,%f\n",xiecharacter.strokeCount,s,d,val,(double)v/d);
        dialog_fenshu d_fenshu(str,this);
        d_fenshu.exec();
        if(val>=90){
            on_bt_next_clicked();
            on_bt_chonghua_clicked();
        }


    }
        }
    catch(...)
    {
        }
        }
}

void Dialog_game::on_bt_pret_clicked()
{

    QString str=ui->le_hanzi->text();
    //printf("%s,%d\n",str,str.length());

    if(hz_index>=str.length())
    {
        hz_index=str.length()-1;
    }
    if(str.length()>0)
    {
        QString s=str.at(hz_index);
        displayCharacter=myrecognizer.findCharacter(s.toStdString());
        if(displayCharacter!=NULL){
            //printf("%s,%d\n",ch->word,ch->strokeCount);
            show_hanzi(displayCharacter);
        }
        hz_index--;
    }
    if(hz_index<0)
        hz_index=0;

}

void Dialog_game::on_bt_bs_clicked()
{
    han_zi:: hanzi* my_zi;
   my_zi= (han_zi:: hanzi*)ui->gridLayout_2->itemAt(0)->widget();
   if(my_zi!=NULL){
    my_zi->hzAnimation2();
    //my_zi->hzAnimation();
   }
}
